package com.skirmisher.inspection.common.utils;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName DealWithLoopUtils
 * @Description 周期二进制转换
 * @Author hanwangxin
 * @Date 2020/12/11 下午3:23
 * @Version 1.0
 */
public class DealWithLoopUtils {

    /**
     * @Author hanwangxin
     * @Description //TODO 将list转换为2进制之和 * @Date 下午3:31 2020/12/11
     * @Param [listInt]
     * @return java.lang.Integer
     **/
    public static Integer list2Int(List<Integer> listInt){
        int value = 0;
        if (listInt == null){
            return value;
        }
        for (Integer intValue : listInt) {
            value |= (int) Math.pow(2,intValue);
        }

        return value;
    }

    /**
     * @Author hanwangxin
     * @Description //TODO 将2进制之和转换为list * @Date 下午3:32 2020/12/11
     * @Param [intValue]
     * @return java.util.List<java.lang.Integer>
     **/
    public static List<Integer> int2List(Integer intValue){

        List<Integer> listValue = new ArrayList<>();
        if (intValue == null || intValue == 0){
            return  listValue;
        }
        for (int i=1;i<= 24;i++){

            int value = (int) Math.pow(2, i);
            if ((intValue & value) > 0){
                listValue.add(i);
            }
        }
        return listValue;
    }

    public static  String list2String(List<Integer> listInt){
        if (listInt ==null || listInt.size() < 0){
            return "";
        }
        String listString = "";
        for (Integer integer : listInt) {
            listString = listString + integer +",";
        }

        return listString;
    }

    public static  List<Integer> string2List (String loopString){
        List<Integer> intList = new ArrayList<>();
        if (StringUtils.isEmpty(loopString)){
            return new ArrayList<>();
        }
        String[] split = loopString.split(",");
        for (String s : split) {
            if (!StringUtils.isEmpty(s)){
                intList.add(Integer.valueOf(s));
            }
        }
        Collections.sort(intList);
        return intList;
    }
}
